export default {
  methods: {
    sendMessageToHomePage(msg) {
      localStorage.setItem('parkinglotSettingMsg', JSON.stringify(msg));
      localStorage.removeItem('parkinglotSettingMsg');
    },
    formatParkBaseInfo(parkBaseInfo, pageChargeList) {
      let copy_parkBaseInfo = JSON.parse(JSON.stringify(parkBaseInfo));
      const xy = copy_parkBaseInfo.uiOpt.gpsXY.split(',');
      copy_parkBaseInfo.gpsX = xy[0];
      copy_parkBaseInfo.gpsY = xy[1];
      delete copy_parkBaseInfo.uiOpt;
      return copy_parkBaseInfo;
    },
    getProtocolKey(parkBaseInfo) {
      return parkBaseInfo.uiOpt.protocolKey;
    },
    formatParkChargeBaseVoList(pageChargeList) {
      return pageChargeList.parkChargeBaseVoList;
    },
    formatParkSpecialChargeVoList(parkSpecialChargeVoList) {
      let ruleList = JSON.parse(JSON.stringify(parkSpecialChargeVoList.ruleList));
      ruleList.forEach(item => {
        if (Array.isArray(item.parkSpecialChargeServiceConditionVo.serviceConditionValue)) {
          item.parkSpecialChargeServiceConditionVo.serviceConditionValue = item.parkSpecialChargeServiceConditionVo.serviceConditionValue.join(',');
        }
      });
      return ruleList;
    },
    formatChannelVoList(channelVoList) {
      const format_channel_list = channelVoList.enter.concat(channelVoList.leave);
      format_channel_list.forEach(item => {
        item.voiceOpenPeriod = item.uiOpt.voiceOpenPeriod.join('-');
        let array = [];
        item.uiOpt.displayContentArray.forEach(item => {
          let o = [];
          item.forEach(sitem => {
            o.push(sitem.key);
          });
          array.push(o);
        });
        item.displayContent.displayContentArray = array;
        item.displayContent = JSON.stringify(item.displayContent);
        item.timeArray = item.uiOpt.voiceOpenPeriod;
      });
      return format_channel_list;
    },
    submitForm() {
      const init_form_data = JSON.parse(JSON.stringify(this.addParkingLotData));
      const submite_form_data = {
        parkBaseInfo: this.formatParkBaseInfo(init_form_data.parkBaseInfo, init_form_data.pageChargeList),
        operatorInitInfo: {
          activationCode: init_form_data.activationCode,
          protocolKey: this.getProtocolKey(init_form_data.parkBaseInfo)
        },
        parkChargeBaseVoList: this.formatParkChargeBaseVoList(init_form_data.pageChargeList),
        parkSpecialChargeVoList: this.formatParkSpecialChargeVoList(init_form_data.parkSpecialChargeVoList),
        channelVoList: this.formatChannelVoList(init_form_data.channelVoList),
        parkCloudDetailVo: {
          defaultProvince: init_form_data.parkBaseInfo.uiOpt.defaultProvince
        }
      };
      // console.log(submite_form_data);
      API.fetchPost('/operatorPark/addOperatorPark', submite_form_data).then(
        res => {
          if (res.status === 1) {
            this.showToast('新增停车场成功', 1, 1500);
            localStorage.removeItem('addParkingLotData');
            this.parkId = res.message;
            setTimeout(() => {
              this.dialogVisible = true;
            }, 1500);
          } else {
            this.showToast(res.message, 1, 2000);
          }
        },
        err => {
          console.log(err);
        }
      );
    },
    dialogClosed() {
      if (this.$route.query.isNeedAddParkinglotFirst) {
        this.$store.commit('SET_NEEPADDPARKINGLOTINGLOT', false);
        this.$root.eventBus.$emit('getMenuList');
        this.$router.push({
          name: 'index'
        });
      } else {
        this.sendMessageToHomePage('updateList');
        window.close();
      }
    },
    downloadChannelCode() {
      window.open(`/mgr/operatorPark/exportChannelCode?parkID=${this.parkId}`);
    }
  }
};
